import * as React from 'react';

import {IView} from '../types/IView';
import {IAttr} from '../types/IAttr';

import {IViewObject} from './IViewObject';
import AbsViewObject from './AbsViewObject';

export class TextViewObject extends AbsViewObject<IView> implements IViewObject<IView> {

  viewType = 'text';

  attrs: IAttr[] = [
    {
      name: 'id',
      display: 'ID',
      type: 'string',
      required: true,
      defaultValGenerator: () => {
        return `textView${this.viewIndex}`;
      }
    },
    {
      name: 'text',
      display: '文本',
      type: 'string',
      required: true,
      defaultValGenerator: () => {
        return `文本${this.viewIndex}`;
      }
    },
    {
      name: 'className',
      display: 'class',
      type: 'string',
      required: false,
      defaultValGenerator: () => {
        return '';
      }
    }
  ];

  create(designId: string): IView {
    return {
      id: this.generateId(),
      viewType: this.viewType,
      designId,
      style: {},
      attrVals: this.createAttrVals(this.attrs)
    };
  }

  render(view: IView, refUpdater: (view: IView, ref: Element) => void): JSX.Element | null {
    const {style, attrVals} = view;

    const elemRefUpdater: React.Ref<Element> = el => refUpdater(view, el);
    const props: React.HTMLAttributes<HTMLElement> = {
      id: attrVals.id,
      className: attrVals.className,
      style: attrVals.className ? {} : style
    };

    return (
      <span ref={elemRefUpdater} {...props}>{attrVals.text}</span>
    );
  }
}

const textViewObject = new TextViewObject();
export default textViewObject;
